<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="650" @ok="handleSubmit" destroyOnClose>
    <BasicForm @register="registerForm" >
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup name="UserForm">
  import {ref, computed, unref,useAttrs} from 'vue';
  import {BasicForm, useForm} from '/@/components/Form/index';
  import { adjustPointFormSchema } from './user.data';
  import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
  import {adjustUserPoint, getUserDetails} from '/@/api/member/user.api';
  import { useMessage } from '/@/hooks/web/useMessage';
  const emit = defineEmits(['success', 'register']);
  const { createMessage } = useMessage();
  const attrs = useAttrs()
  const rowId = ref('');
  const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
    labelWidth: 100,
    schemas: adjustPointFormSchema,
    showActionButtonGroup: false,
    wrapperCol: {lg: 24, sm: 24} 
  });
  
  const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {
    await resetFields();
    let showFooter = data?.showFooter ?? true
    setDrawerProps({ confirmLoading: false, showFooter, loading: true })
    try {
      rowId.value = data.record.id;
      try {
        data.record = await getUserDetails({id: data.record.id});
      } catch (error) {
      }
      setFieldsValue({ ...data.record })
    } finally {
      setDrawerProps({ loading: false })
    }

    setProps({ disabled: !showFooter })
  });

  const getTitle = computed(() => '调整积分');

  async function handleSubmit() {
    try {
      let values = await validate();
      setDrawerProps({confirmLoading: true});
      await adjustUserPoint(values);
      closeDrawer();
      createMessage.success('调整成功')
      emit('success');
    } finally {
      setDrawerProps({confirmLoading: false});
    }
  }
</script>
